package com.chenss.dragonser.app;

import android.content.Context;
import android.text.TextUtils;

import com.chenss.dragonser.model.UserInfoModel;
import com.chenss.dragonser.utils.GsonUtil;
import com.chenss.dragonser.utils.SharedPreferencesUtil;


/**
 * 用户信息本地化
 */

public class AppInfo {
    private static UserInfoModel bean;


    /**
     * 保存用户信息
     *
     * @param context
     * @param userInfoBean
     */
    public static void setUser(Context context, UserInfoModel userInfoBean) {

        if(null==userInfoBean){
            SharedPreferencesUtil.put(context, "userInfo", "");
        }else{
            String userInfoJson = GsonUtil.GsonString(userInfoBean);
            SharedPreferencesUtil.put(context, "userInfo", userInfoJson);
        }

        bean = userInfoBean;
    }

    /**
     * 获取用户信息
     *
     * @param context
     * @return
     */
    public static UserInfoModel getUser(Context context) {
        if (bean == null) {
            String json = (String) SharedPreferencesUtil.get(context.getApplicationContext(), "userInfo", "");
            if (!TextUtils.isEmpty(json)) {
                bean = GsonUtil.GsonToBean(json, UserInfoModel.class);
            }
        }
        return bean;
    }

    /**
     * 清除用户信息 如果isNull 为true 将UserInfoBean 对象赋为null
     *
     * @param context
     * @param isNull
     */
    public static void clearUserInfo(Context context, boolean isNull) {
        SharedPreferencesUtil.put(context.getApplicationContext(),
                "userInfo", "");
        if (isNull) {
            bean = null;
        }
    }
}

